﻿package com.library.utils.vily
{
	
	//org.vily.primitives.Vector_2D
	
	public class Vector_2D{
		/*	-----------------------------------------
			本类实现2D空间当中矢量的基本属性和行为
			-----------------------------------------	*/
		private var __x:Number = 0;
		private var __y:Number = 0;
		public var x:Number = 0;
		public var y:Number = 0;
		public function Vector_2D(px:Number, py:Number) {
			__x = x = px;
			__y = y = py;
		}
		public function toInit():void{
			x = __x;
			y = __y;
		}
		public function setTo(px:Number, py:Number):void {
			x = px;
			y = py;
		}
		public function copy(v:Vector_2D):void {
			x = v.x;
			y = v.y;
		}
		public function clone():Vector_2D {
			return new Vector_2D(this.x,this.y);
		}
	
		public function dot(v:Vector_2D):Number {
			return x * v.x + y * v.y;
		}
		public function dotNew(v:Vector_2D):Vector_2D {
			return new Vector_2D(x * v.x, y * v.y);
		}
	
		public function cross(v:Vector_2D):Number {
			/**
				顺时针为正，逆时针为负
			*/
			return x * v.y - y * v.x;
		}
	
		public function plus(v:Vector_2D):void {
			x += v.x;
			y += v.y;
		}
		public function plusNew(v:Vector_2D):Vector_2D {
			return new Vector_2D(x + v.x, y + v.y); 
		}

		public function minus(v:Vector_2D):void {
			x -= v.x;
			y -= v.y;			
		}
		public function minusNew(v:Vector_2D):Vector_2D {
			return new Vector_2D(x - v.x, y - v.y);    
		}

		public function mult(s:Number):void {
			x *= s;
			y *= s;			
		}
		public function multNew(s:Number):Vector_2D {
			return new Vector_2D(x * s, y * s);
		}
		public function divide(s:Number):Vector_2D{
			if(s<=0){
				trace("分母不能为零");
			}else{
				x /= s;
				y /= s;
			}
			return this;
		}
		public function divideNew(s:Number):Vector_2D{
			if(s<=0){
				trace("分母不能为零");
			}else{
				return new Vector_2D(x/s,y/s);
			}
			return clone();
		}
		
		//规范化
		public function normalize():Vector_2D {
	  		var mag:Number = Math.sqrt(x * x + y * y);
	  		if(mag<=0){
				x = 0;
				y = 0;
				trace("Error:publicOrg.vily.primitives.Vector_2D::normalize()零矢量不能规范化");
				return this;
			}
	  		x /= mag;
			y /= mag;
			return this;
		}
		//幅 , 模
		public function magnitude():Number {
			return Math.sqrt(x * x + y * y);
		}
		//返回模的平方值
		public function squareMagnitude():Number {			
			return (x * x + y * y);
		}
		
		public function getRadian():Number{
			//返回相对于(start_x,start_y)点的角度的弧度值
			//计算现在位置变化后的方向弧度值
			var radians:Number = Math.atan(y/x);
			if(isNaN(radians)){
				return Math.PI/2;
			}
    		if(y>=0 && x>=0){
	 			return radians;
			}else if(y<0 && x>=0){
				radians = 2*Math.PI+radians;
				return radians;
			}else{
				radians = Math.PI+radians;
				return radians;
			}
		}
		public function getKNum():Number{
			//返回相对于(start_x,start_y)点的角度的弧度值
			//计算现在位置变化后的方向弧度值
			if(x == 0 && y>0){
				return Number.POSITIVE_INFINITY;
			}else if(x == 0 && y<0){
				return Number.NEGATIVE_INFINITY;
			}
			return (y/x);
		}
		public function getAngle():Number{
			//返回相对于(start_x,start_y)点的角度
			//计算现在位置变化后的方向角度值
			var angle:Number = Math.atan(y/x) * 180/Math.PI;	
    		if(y>=0 && x>=0){	 	
				return Math.round(angle);
			}else if(y<0 && x>=0){
				return Math.round(360+angle);
			}else{		
				return Math.round(180+angle);	
			}
		}
		public function toString():String{
			return "x: "+ x+"  y: "+y;
		}
	}
}
